package org.example.spring.ioc.config;


import org.example.spring.ioc.datasource.MyDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class DataSourceConfig {
    //1.定义环境标识：自定义【dev\test\prod】;默认【default】
    //2.激活环境标识：
    //          明确告诉spring当前处于什么环境:配置文件中写spring.profiles.active=prod
    //          不说就是默认环境

    //利用条件注解，旨在某种环境下激活一个组件
    @Profile({"dev","default"})
    @Bean
    public MyDataSource dev(){
        MyDataSource myDataSource = new MyDataSource();
        myDataSource.setUserName("dev_user");
        myDataSource.setPassword("<DEV_PASSWORD>");
        myDataSource.setUrl("jdbc:mysql://localhost:3306/dev");
        return myDataSource;
    }

    @Profile("test")
    @Bean
    public MyDataSource test(){
        MyDataSource myDataSource = new MyDataSource();
        myDataSource.setUserName("test_user");
        myDataSource.setPassword("<TEST_PASSWORD>");
        myDataSource.setUrl("jdbc:mysql://localhost:3306/test");
        return myDataSource;
    }

    @Profile("prod")
    @Bean
    public MyDataSource production(){
        MyDataSource myDataSource = new MyDataSource();
        myDataSource.setUserName("production_user");
        myDataSource.setPassword("<PRODUCTION_PASSWORD>");
        myDataSource.setUrl("jdbc:mysql://localhost:3306/production");
        return myDataSource;
    }
}
